home *** CD-ROM | disk | FTP | other *** search
/ Explorer - Mosaic & Web / Explorer - Mosaic & Web.iso / helpers / ghostvew / src / doc2tex.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-05-30  |  5.3 KB  |  254 lines

  1. /*
  2.  * doc2tex.c  -- program to convert Gnuplot .DOC format to LaTeX document
  3.  * Also will work for VMS .HLP files. 
  4.  * Modified by Russell Lang from hlp2ms.c by Thomas Williams 
  5.  * Extended by David Kotz to support quotes ("), backquotes, tables.
  6.  *
  7.  * usage:  doc2tex [file.doc [file.tex]]
  8.  *
  9.  *   where file.doc is a Gnuplot .DOC file, and file.tex will be an
  10.  *     article document suitable for printing with LaTeX.
  11.  *
  12.  * typical usage for GNUPLOT:
  13.  *
  14.  *   doc2tex gnuplot.doc gnuplot.tex 
  15.  *   latex gnuplot.tex ; latex gnuplot.tex
  16.  */
  17.  
  18. #include <stdio.h>
  19. #include <ctype.h>
  20.  
  21. #define MAX_NAME_LEN    256
  22. #define MAX_LINE_LEN    256
  23. #define TRUE 1
  24. #define FALSE 0
  25.  
  26. typedef int boolean;
  27.  
  28. boolean intable = FALSE;
  29. boolean verb = FALSE;
  30.  
  31. main(argc,argv)
  32. int argc;
  33. char **argv;
  34. {
  35. FILE * infile;
  36. FILE * outfile;
  37.     infile = stdin;
  38.     outfile = stdout;
  39.     if (argc > 3) {
  40.         fprintf(stderr,"Usage: %s [infile [outfile]]\n", argv[0]);
  41.         exit(1);
  42.     }
  43.     if (argc >= 2) 
  44.         if ( (infile = fopen(argv[1],"r")) == (FILE *)NULL) {
  45.             fprintf(stderr,"%s: Can't open %s for reading\n",
  46.                 argv[0], argv[1]);
  47.             exit(1);
  48.         }
  49.     if (argc == 3)
  50.         if ( (outfile = fopen(argv[2],"w")) == (FILE *)NULL) {
  51.             fprintf(stderr,"%s: Can't open %s for writing\n",
  52.                 argv[0], argv[2]);
  53.         }
  54.     
  55.     init(outfile);
  56.     convert(infile,outfile);
  57.     finish(outfile);
  58.     exit(0);
  59. }
  60.  
  61.  
  62. init(b)
  63. FILE *b;
  64. {
  65.     (void) fputs("\\input{titlepag.tex}\n",b);
  66. }
  67.  
  68.  
  69. convert(a,b)
  70.     FILE *a,*b;
  71. {
  72.     static char line[MAX_LINE_LEN];
  73.  
  74.     while (fgets(line,MAX_LINE_LEN,a)) {
  75.        process_line(line, b);
  76.     }
  77. }
  78.  
  79. process_line(line, b)
  80.     char *line;
  81.     FILE *b;
  82. {
  83.     switch(line[0]) {        /* control character */
  84.        case '?': {            /* interactive help entry */
  85.           break;            /* ignore */
  86.        }
  87.        case '@': {            /* start/end table */
  88.           if (intable) {
  89.              (void) fputs("\\hline\n\\end{tabular}\n", b);
  90.              (void) fputs("\\end{center}\n",b);
  91.              intable = FALSE;
  92.           } else {
  93.              if (verb) {
  94.                 (void) fputs("\\end{verbatim}\n",b);
  95.                 verb=FALSE;
  96.              } 
  97.              (void) fputs("\n\\begin{center}\n", b);
  98.              (void) fputs("\\begin{tabular}{|ccl|} \\hline\n", b);
  99.              intable = TRUE;
  100.           }
  101.           /* ignore rest of line */
  102.           break;
  103.        }
  104.        case '#': {            /* latex table entry */
  105.           if (intable)
  106.             (void) fputs(line+1, b); /* copy directly */
  107.           else
  108.             fprintf(stderr, "error: # line found outside of table\n");
  109.           break;
  110.        }
  111.        case '%': {            /* troff table entry */
  112.           break;            /* ignore */
  113.        }
  114.        case '\n':            /* empty text line */
  115.        case ' ': {            /* normal text line */
  116.           if (intable)
  117.             break;        /* ignore while in table */
  118.           if (line[1] == ' ') {
  119.              /* verbatim mode */
  120.              if (!verb) {
  121.                 (void) fputs("\\begin{verbatim}\n",b);
  122.                 verb=TRUE;
  123.              }
  124.              (void) fputs(line+1,b); 
  125.           } else {
  126.              if (verb) {
  127.                 (void) fputs("\\end{verbatim}\n",b);
  128.                 verb=FALSE;
  129.              } 
  130.              if (line[0] == '\n')
  131.                puttex(line,b); /* handle totally blank line */
  132.              else
  133.                puttex(line+1,b);
  134.           }
  135.           break;
  136.        }
  137.        default: {
  138.           if (isdigit(line[0])) { /* start of section */
  139.              if (!intable)    /* ignore while in table */
  140.                section(line, b);
  141.           } else
  142.             fprintf(stderr, "unknown control code '%c' in column 1\n", 
  143.                   line[0]);
  144.           break;
  145.        }
  146.     }
  147. }
  148.  
  149. /* process a line with a digit control char */
  150. /* starts a new [sub]section */
  151.  
  152. section(line, b)
  153.     char *line;
  154.     FILE *b;
  155. {
  156.     static char string[MAX_LINE_LEN];
  157.     int sh_i;
  158.  
  159.     if (verb) {
  160.        (void) fputs("\\end{verbatim}\n",b);
  161.        verb=FALSE;
  162.     } 
  163.     (void) sscanf(line,"%d %[^\n]s",&sh_i,string);
  164.     switch(sh_i)
  165.      {
  166.         case 1: 
  167.         (void) fprintf(b,"\\section{");
  168.         break;
  169.         case 2: 
  170.         (void) fprintf(b,"\\section{");
  171.         break;
  172.         case 3:
  173.         (void) fprintf(b,"\\subsection{");
  174.         break;
  175.         case 4: 
  176.         (void) fprintf(b,"\\subsubsection{");
  177.         break;
  178.         default:
  179.         case 5: 
  180.         (void) fprintf(b,"\\paragraph{");
  181.         break;
  182.      }
  183.     if (islower(string[0]))
  184.      string[0] = toupper(string[0]);
  185.     puttex(string,b);
  186.     (void) fprintf(b,"}\n");
  187. }
  188.  
  189. /* put text in string str to file while buffering special TeX characters */
  190. puttex(str,file)
  191. FILE *file;
  192. register char *str;
  193. {
  194. register char ch;
  195. static boolean inquote = FALSE;
  196.  
  197.      while( (ch = *str++) != '\0') {
  198.          switch(ch) {
  199.              case '#':
  200.              case '$':
  201.              case '%':
  202.              case '&':
  203.              case '_':
  204.              case '{':
  205.              case '}':
  206.                  (void) fputc('\\',file);
  207.                  (void) fputc(ch,file);
  208.                  break;
  209.              case '\\':
  210.                  (void) fputs("$\\backslash$",file);
  211.                  break;
  212.              case '~':
  213.                  (void) fputs("\\~{\\ }",file);
  214.                  break;
  215.              case '^':
  216.                  (void) fputs("\\verb+^+",file);
  217.                  break;
  218.              case '>':
  219.              case '<':
  220.              case '|':
  221.                  (void) fputc('$',file);
  222.                  (void) fputc(ch,file);
  223.                  (void) fputc('$',file);
  224.                  break;
  225.              case '"': 
  226.                  /* peek at next character: if space, end of quote */
  227.                  if (*str == '\0' || isspace(*str) || ispunct(*str))
  228.                    (void) fputs("''", file);
  229.                  else
  230.                    (void) fputs("``", file);
  231.                  break;
  232.              case '`':    /* backquotes mean boldface */
  233.                  if (inquote) {
  234.                     fputs("}", file);
  235.                     inquote = FALSE;
  236.                  } else {
  237.                     fputs("{\\bf ", file);
  238.                     inquote = TRUE;
  239.                  }
  240.                  break;
  241.              default:
  242.                  (void) fputc(ch,file);
  243.                  break;
  244.          }
  245.      }
  246. }
  247.  
  248.  
  249. finish(b)
  250. FILE *b;
  251. {
  252.     (void) fputs("\\end{document}\n",b);
  253. }
  254.